package org.jeecg.modules.door.controller;
import org.jeecg.modules.door.entity.LwgyJointDoor;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpEntity;
import org.springframework.http.HttpHeaders;
import org.springframework.http.HttpStatus;
import org.springframework.web.bind.annotation.*;
import org.springframework.http.ResponseEntity;
import org.springframework.web.client.RestTemplate;

import java.util.HashMap;
import java.util.Map;
//未启用
// 向netty 发送的http请求，netty 接收与串口的数据必须俩端口 ，同一个端口接受两种指令，测试了几次无法很好的区分，
// 如果要启用这个方法，可以试着与串口接口分成两个端口接收
@RestController
@RequestMapping("/api")
public class ClientController {

    @Autowired
    private RestTemplate  restTemplate;

    private String ip;
    private String port;

    @PostMapping("/queryDoorInfo")
    public ResponseEntity<String> sendQuery(@RequestBody LwgyJointDoor lwgyJointDoor) throws Exception {
        try{
            // 构建请求体
            Map<String, String> requestBody = new HashMap<>();
            requestBody.put("method", "obtain");
            requestBody.put("doorNo", lwgyJointDoor.getDoorNo());

            // 发送HTTP POST请求到Netty服务器
            String url = "http://192.168.0.120:8888"; // Netty服务器的URL
            HttpEntity<Map<String, String>> request = new HttpEntity<>(requestBody, new HttpHeaders());
            ResponseEntity<String> response = restTemplate.postForEntity(url, request, String.class);

            // 处理响应
            if (response.getStatusCode() == HttpStatus.OK) {
                // 更新页面或执行其他逻辑（这里只是打印响应）
                System.out.println("Response from Netty: " + response.getBody());
                return ResponseEntity.ok("Request sent successfully, response: " + response.getBody());
            } else {
                return ResponseEntity.status(response.getStatusCode()).body("Failed to send request");
            }
        }catch (Exception e){
            System.out.println("e=========:"+e);
            return ResponseEntity.ok("Request sent successfully, response: ");
        }



        /*    @GetMapping("/sendCommand")
    public String sendCommand(@RequestParam String deviceId, @RequestParam String hexCommand) {
        String url = "http://localhost:8080"; // Netty服务器地址
        String requestContent = deviceId + "," + hexCommand;

        // 使用RestTemplate发送HTTP请求
        RestTemplate restTemplate = new RestTemplate();
        String response = restTemplate.postForObject(url, requestContent, String.class);
  */
    }
}


